package com.example.androidad;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.*;

import org.json.JSONException;

import java.util.Locale;

public class AdUtil {
    public static String appid="";
    public static String appsecret="";
    public static String lan="";
    public static void show(Context context){
        if (appid.equals("")){
            Log.e("hpytg",StringUtil.getErrMsg(ErrCode.APP_ID_EMPTY,"AppID 为空"));
            return;
        }
        if (appsecret.equals("")){
            Log.e("hpytg",StringUtil.getErrMsg(ErrCode.APP_SECRET_KEY_EMPTY,"AppSecretKey 为空"));
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        AlertDialog alertDialog = builder.create();
        View dialogView = View.inflate(context,R.layout.activity_main,null);
        ImageView imageView = (ImageView) dialogView.findViewById(R.id.imageView);
        ImageView imageView1 = (ImageView) dialogView.findViewById(R.id.imageView1);
        TextView textView = (TextView) dialogView.findViewById(R.id.textView);
        TextView textView2 = (TextView) dialogView.findViewById(R.id.textView2);
        TextView textView3 = (TextView) dialogView.findViewById(R.id.textView3);
        Button button = (Button)dialogView.findViewById(R.id.button);
        alertDialog.setView(dialogView);
        alertDialog.getWindow().setLayout((int) (ScreenUtil.getScreenWidth(context)*0.8), (int) (ScreenUtil.getScreenHeight(context)*0.8));
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String response = HttpUtil.postrequest();
                    Gson gson = new Gson();
                    AdModel adModel = gson.fromJson(response, AdModel.class);
                    Glide.with(context)
                            .load(adModel.getIcon())
                            .into(imageView);
                    Glide.with(context)
                            .load(adModel.getImage())
                            .into(imageView1);
textView.setText(adModel.getTitle());
textView2.setText(adModel.getSubtitle());
textView3.setText(adModel.getDescription());
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(adModel.getUrl()));
        context.startActivity(intent);
    }
});
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();



        Resources resources = context.getResources();
        Drawable dialogDrawable = resources.getDrawable(R.drawable.roundedcornor);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        dialogDrawable.setBounds(0, 0, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);


        alertDialog.getWindow().setBackgroundDrawable(dialogDrawable);

        alertDialog.show();
    }

    public static void init(Context context,String appiddata,String appsecretdata){
        Resources resources = context.getResources();

        // 获取Configuration对象
        Configuration configuration = resources.getConfiguration();

        // 获取当前Locale对象
        Locale locale = configuration.getLocales().get(0); // 从API 24开始使用getLocales().get(0)获取主要locale，之前版本使用configuration.locale;

        // 获取语言代码
        String language = locale.getLanguage();

        // 获取国家/地区代码（如果存在）
        String country = locale.getCountry();

        lan = language;
        appid = appiddata;
        appsecret = appsecretdata;
    }
}
